iT邦幫忙

0

Day 19視窗應用程式基礎通用控制項:Label (標籤) & Button (按鈕)

c#
  • 分享至 

  • xImage
  •  

Label(標籤)

  1. Label的用途
    Label是Windows Forms常用的控制項之一
    主要功能:
  • 顯示文字內容(提示、說明、結果…)
  • 通常用來搭配其他控制項(例如 TextBox、Button)提供說明
  • 不能被使用者編輯(和 TextBox 不同)
  1. 常用屬性 Properties(描述這個物件的外觀)
    https://ithelp.ithome.com.tw/upload/images/20251005/20178925SboTdC1Dci.png

  2. 常用方法 Methods(在物件完成某件事或某項目標)
    Label 本身方法不多,因為它只是顯示文字,常用的有:

  • .Show() → 顯示Label
  • .Hide() → 隱藏Label
  • .Refresh() → 重新繪製控制項(當你改了屬性時可立即更新)
  1. 常見事件 Events
    Label不是互動性控制項,所以事件比較少,但也能用:
  • Click:點擊Label時觸發
  • MouseEnter:滑鼠移入時觸發
  • MouseLeave:滑鼠移開時觸發

範例
https://ithelp.ithome.com.tw/upload/images/20251005/20178925qjI60BYIs4.png

一開始顯示文字背景為黃色
https://ithelp.ithome.com.tw/upload/images/20251005/20178925AKIqgzB8s4.png

滑鼠移到label上,背景變為綠色
https://ithelp.ithome.com.tw/upload/images/20251005/20178925zktQpKFd5i.png

點擊label,文字改為-你點擊了Label!-
https://ithelp.ithome.com.tw/upload/images/20251005/201789254csxxpWz1m.png

Button(按鈕)

  1. Button 的用途
    Button是一種互動式控制項
    主要功能:
  • 讓使用者點擊後,觸發一段程式碼(事件)
  • 常用於 提交資料、執行命令、開啟視窗 等動作
  1. 常用屬性 (Properties)
    https://ithelp.ithome.com.tw/upload/images/20251005/20178925ZRMPJjMgi5.png

  2. 常用方法 (Methods)
    Button本身的方法不多,常用的有:

  • .PerformClick() → 模擬點擊按鈕
  • .Focus() → 讓按鈕取得焦點
  • .Show() / .Hide() → 顯示或隱藏按鈕
  1. 常見事件 (Events)
  • Click:使用者點擊按鈕時觸發
  • MouseEnter:滑鼠移入按鈕時觸發
  • MouseLeave:滑鼠移開按鈕時觸發
  • MouseDown:滑鼠按下時觸發
  • MouseUp:滑鼠放開時觸發
  • KeyDown:鍵盤按下某個鍵時觸發(當按鈕有焦點)

範例
https://ithelp.ithome.com.tw/upload/images/20251005/20178925d1Fr6Yyhe5.png

一開始顯示按鈕背景為藍色
https://ithelp.ithome.com.tw/upload/images/20251005/201789250iG6IHKPBB.png

滑鼠移到按鈕上,背景變為綠色
https://ithelp.ithome.com.tw/upload/images/20251005/20178925jsHO3BzoZ9.png

點擊按鈕後,label改為-你點擊了按鈕!-
https://ithelp.ithome.com.tw/upload/images/20251005/20178925TZieQBaJlw.png


圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言